//
//  MainViewController.m
//  UI09_4_Cell 高度自动调整
//
//  Created by Scott on 15/5/7.
//  Copyright (c) 2015年 www.lanou3g.com 蓝欧3G. All rights reserved.
//

#import "MainViewController.h"
#import "MyCell_1.h"

@interface MainViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arr;

@end

@implementation MainViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.arr = [NSMutableArray arrayWithObjects:@"北京", @"沈阳", @"大连", @"河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北河北", @"河南", @"黑龙江", @"吉林", @"辽宁北京北京北京北京北京北京北京北京北京北京北京北京北京北京北京北京北京北京北京北京北京北京北京",  nil];
    }
    return self;
}

- (void)dealloc
{
    [_arr release];
    [super dealloc];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"自适应高度";
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    [self.view addSubview:tableView];
    [tableView release];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorColor = [UIColor greenColor];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyCell_1 *myCell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (myCell == nil) {
        myCell = [[[MyCell_1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"] autorelease];
    }
    
    
    myCell.cellLable.text = [self.arr objectAtIndex:indexPath.row];
    myCell.cellLable.backgroundColor = [UIColor whiteColor];
    return myCell;
    
}

#pragma mark - 知识点1 cell 自适应高度(字符串)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /** 
     * 详解 
     * 行的高度根据cell中文本框的高度确定, 所以这个地方调用自定义Cell的方法计算高度
     * 但此处没有自定义cell 的对象, 故不能调用实例方法, 调用类方法.
     * 所以在自定义的cell中写一个类的方法, 用来计算文本框的高度
     *
     * 详见Mycell类
     *
     */
    
   CGFloat height =  [[MyCell_1 class] heightWithText:[self.arr objectAtIndex:indexPath.row]];
    
//    CGFloat height2 = [MyCell_1 heightWithText:[self.arr objectAtIndex:indexPath.row]];
    
    
    return height + 20;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
